﻿using StackAttack_WindowsFormApp.DrawLogic;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Core.Generators;
using Core.Infarastructure.Structs;
using Core.Helpers;
using Core.Logic;
using StackAttack_WindowsFormApp.Engine;
using System.Windows.Forms;
using StackAttack_WindowsFormApp.PlayersData;
using StackAttack_WindowsFormApp.Helpers;
using StackAttack_WindowsFormApp.Helper_Forms;

namespace StackAttack_WindowsFormApp
{
    public static class StackAttackManager
    {
        public static bool isThisFirstInitialization {get; set;}

        private static Graphics _graphics = null;

        private static WindowsFormEngine drawingEngine;

        private static StackCreator _stackCreator;
        private static HeroCreator _heroCreator;

        static StackAttackManager()
        {

            isThisFirstInitialization = true;
            _heroCreator = new HeroCreator();
            _stackCreator = new StackCreator();

        }
        /// <summary>
        /// Provide logic for start game
        /// </summary>
        /// <param name="app"></param>
        public static void Start(MainApp app, Graphics graphics, DrawingFieldStructure drawingField)
        {
            
            

            app.DrawingBox.Image = null;
            app.DrawingBox.BackColor = Color.Transparent;
            app.DrawingBox.Controls.Clear();

            
            //app.DrawingBox.Image = Image.FromFile(PathFinder.GetFullPathToFile("Images\\background.png"));
           
                StackGenerator sg = new StackGenerator();
                
                
                EnterNameForm enterName = new EnterNameForm(app);
                enterName.ShowDialog();

                _graphics = graphics;

                var firstCollection = sg.GenerateFirstCollection(drawingField, Settings.MaxStacksCount);

                _stackCreator.FirstInitialize(_graphics, firstCollection);

                _heroCreator.Create(_graphics, PutHeroOnGameBoard.Put(firstCollection));

                isThisFirstInitialization = false;
                System.Drawing.Point point = new System.Drawing.Point(PutHeroOnGameBoard.Put(firstCollection).X, PutHeroOnGameBoard.Put(firstCollection).Y);
                drawingEngine = new WindowsFormEngine(app, graphics, firstCollection, point);
            
            

                
        }

        
        /// <summary>
        /// Provide a logic to pause the game
        /// </summary>
        /// <param name="app"></param>
        public static void Pause(MainApp app)
        {
            if (drawingEngine.Paused)
            {
                drawingEngine.checkMoving.Start();
                drawingEngine.stackFalling.Start();
                if (drawingEngine.heroMoving.Tag == "enabled")
                {
                    drawingEngine.heroMoving.Start();
                }
                if (drawingEngine.heroFalling.Tag == "enabled")
                {
                    drawingEngine.heroFalling.Start();
                }

                drawingEngine.Paused = false;
                return;

            }
        
            if (drawingEngine!=null)
            {
                drawingEngine.AllTimersStop();
            }
        }


        
    }
}
